<template>

  <div class="app-container" style="font-size:12px">
    <div class="filter-container">
      <el-date-picker v-model="listQuery.date1" style="width:140px;" format="yyyy-MM-dd" type="datetime" placeholder="订单开始日" />
      <el-date-picker v-model="listQuery.date2" style="width:140px" format="yyyy-MM-dd" type="datetime" placeholder="订单结束日" />
      <el-input v-model="listQuery.customer" placeholder="客户姓名" style="width: 100px;margin-top:6px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.merchant" placeholder="选择商户" filterable class="filter-item" style="width:110px;margin-top:6px">
        <el-option label="选择商户" value="" />
        <el-option
          v-for="item in merchant_list"
          :key="item.merchant_id"
          :label="item.merchant_name"
          :value="item.merchant_id"
        />
      </el-select>
      <el-select v-model="listQuery.sale_id" placeholder="选择销售" filterable class="filter-item" style="width:110px;margin-top:6px">
        <el-option label="选择销售" value="" />
        <el-option
          v-for="item in sale_list"
          :key="item.sale_id"
          :label="item.real_name"
          :value="item.sale_id"
        />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:6px" @click="handleFilter">查询</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="excelout">导出</el-button>
      <!-- <excel ></excel> -->
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;margin-top:20px">
      <el-table-column label="签约时间" align="center" width="100px">
        <template slot-scope="{row}">
          <!-- {{ row.create_time|formatDate}} -->
          {{ row.pass_time }}
        </template>
      </el-table-column>
      <el-table-column label="商户名称" align="center" width="300">
        <template slot-scope="{row}">
          <span>{{ row.sort_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户电话" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column label="项目金额" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租金收益" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.all_server }}</span>
        </template>
      </el-table-column>

      <el-table-column label="收款方式" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.pay_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款时间" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.pay_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="{row}">
          <span>{{ row.desc }}</span>
        </template>
      </el-table-column>

      <el-table-column label="签单人" align="center" width="70">
        <template slot-scope="{row}">
          <span>{{ row.sale_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" width="110">
        <template slot-scope="{row}">
          <span>{{ row.admin_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-dropdown :hide-on-click="false">
            <span class="el-dropdown-link" style="color:#67C23A">
              更多操作
              <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button type="primary" size="mini" @click="serverMoney(row)">
                  租金收益
                </el-button>
              </el-dropdown-item>
              <!-- <el-dropdown-item>
                <el-button  type="warning" size="mini" @click="serverMoney(row.order_id)">
                  返服务费
                </el-button>
              </el-dropdown-item>               -->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog title="租金收益" :visible.sync="serverAble" style="width:1000px;margin:auto">
      <el-form ref="dataForm" :model="form" label-width="120px">
        <el-form-item label="租金收益">
          <el-input v-model="form.serverMoneyNun" type="number" />
        </el-form-item>
        <el-form-item label="收款方式">
          <el-select v-model="form.payway" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in payway"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收款时间">

          <el-date-picker v-model="form.pay_time" type="date" placeholder="选择日期" style="width: 100%;" />

        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.desc" type="text" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="serverMediateAllMoneySubmit()">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>

import {
  getPayWay, getRegion, serverMediateAllMoneySubmit, serviceMediateList, exceloutmediate
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantListOnlyMerchant } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import excelout from '@/components/excelout/index.vue'
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination, excelout
  },
  directives: {
    waves
  },
  filters: {
   	formatDate(time) {
   	        time = time * 1000
   	        const date = new Date(time)

   	        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
   	      }
  },
  data() {
    return {
      form: {
        payway: '',
        order_id: '',
        serverMoneyNun: '',
        serverMoneyBack: '',
        desc: '',
        pay_time: ''
      },
      tableKey: 0,
      returnOrderAble: false,
      channelChooseAble: false,
      serverAble: false,
      title: ['账户列表', '已选列表'],
      tableRecord: 1,
      list: null,
      channelList: [],
      allcount: [],
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      city: [],
      choose: [],
      payway: [],
      listQuery: {
        page: 1,
        limit: 10,
        customer: '',
        rebackdate: '',
        city: '',
        quyu: '',
        merchant: '',
        sale_id: '',
        b_model: '',
        order_status: '',
        excel: 0,
        date1: '',
        date2: '',
        sex: '',
        guarantee: ''
      },
      listRecordQuery: {
        page: 1,
        limit: 20,
        order_id: ''
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      textMap: {
        record: '订单记录',
        create: 'Create'
      },
      merchant_list: [],
      sale_list: [],
      dialogRecordVisible: false,
      dialogStatus: '',
      downloadLoading: false
    }
  },
  created() {
    this.cacheQueryFormat()
    this.getList()
    this.getMerchantList()
    this.getPayWay()
    // 添加钩子，页面刷新
    window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  destroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('financeList')
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('financeList')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    getList() {
      sessionStorage.setItem('financeList', JSON.stringify(this.listQuery))
      serviceMediateList(this.listQuery).then(response => {
        if (!this.listQuery.excel) {
          this.list = response.data.data
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 0.1 * 1000)
        } else {
          this.listQuery.excel = 0
        }
      })
    },

    returnOrderSubmit() {
      returnOrderSubmit(this.form).then(response => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.returnOrderAble = false
      })
    },
    serverMediateAllMoneySubmit() {
      // console.log(this.form);return;
      serverMediateAllMoneySubmit(this.form).then(response => {
        this.$notify({
          title: '提示',
          message: '添加成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.serverAble = false
      })
    },
    handleFilter() {
      this.listLoading = true
      this.listQuery.page = 1
      this.getList()
    },
    serverMoney(row) {
      this.form.order_id = row.order_id
      this.form.payway = row.pay_way
      this.form.serverMoneyNun = row.all_server
      this.form.serverMoneyBack = row.back_server
      this.form.desc = row.desc
      this.form.return = row.return
      this.form.buyout = row.buyout
      this.form.hold_first = row.hold_first
      this.form.bond = row.bond
      this.form.pay_time = row.pay_time
      this.serverAble = true
    },
    excelout() {
      this.listLoading = true
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      exceloutmediate(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    getPayWay() {
      getPayWay().then(res => {
        this.payway = res.data
      })
    },
    getRegionList() {
      getRegion(this.listQuery).then(response => {
        if (this.listQuery.city) {
          this.quyu = response.data
        } else {
          this.city = response.data
        }
      })
    },
    getMerchantList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.merchant_list = response.data.merchant.data
        this.sale_list = response.data.sale.data
      })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  td{padding: 0;}
  select{
    width:100px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
   .el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .el-table .default-row {
    background: #ccc;
  }
</style>
